
package com.zjff.myshop.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.zjff.myshop.admin.pojo.vo.HomeCarouselVO;
import com.zjff.myshop.admin.pojo.vo.HomeGoodsVO;
import com.zjff.myshop.entity.Home;
import com.zjff.myshop.exception.BusinessException;
import com.zjff.myshop.mapper.GoodsMapper;
import com.zjff.myshop.mapper.HomeMapper;
import com.zjff.myshop.admin.service.HomeService;
import com.zjff.myshop.util.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//保持容器中bean的名称不重复，对其中的一个bean进行自定义命名。
@Service("adminHomeServiceImpl")
public class HomeServiceImpl implements HomeService {

    @Resource
    private HomeMapper homeMapper;

    @Resource
    private GoodsMapper goodsMapper;


    @Override
    public boolean insert(Home home) {
        //检查商品id，确保首页配置得商品是存在的
        if (goodsMapper.selectByPrimaryKey(home.getGoodsId()) == null) {
            throw new BusinessException("商品不存在!");
        }
        //是否已存在相同记录（分类id + 商品id)
        if (this.getByTypeIdAndGoodsId(home.getTypeId(), home.getGoodsId()) != null) {
            throw new BusinessException("已存在相同的首页配置项！");
        }

        return homeMapper.insert(home) > 0;
    }

    @Override
    public boolean deleteById(Integer id) {
        //检查记录是否存在
        Home home = homeMapper.selectByPrimaryKey(id);
        if (home == null) {
            throw new BusinessException("数据不存在!");
        }

        return homeMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public boolean deleteBatch(Integer[] ids) {
        //删除数据
        return homeMapper.deleteBatch(ids) > 0;
    }

    @Override
    public boolean update(Home home) {
        //检查商品goodsId,确保商品是存在的
        if (goodsMapper.selectByPrimaryKey(home.getGoodsId()) == null) {
            throw new BusinessException("商品不存在!");
        }
        //检查对应记录是否存在（id）
        Home temp = homeMapper.selectByPrimaryKey(home.getId());
        if (temp == null) {
            throw new BusinessException("数据不存在!");
        }
        //是否已存在相同记录（分类id + 商品id)
        temp = this.getByTypeIdAndGoodsId(home.getTypeId(), home.getGoodsId());
        if (temp != null && !temp.getId().equals(home.getId())) {
            //goodsId相同且不同id 不能继续更改
            throw new BusinessException("已存在相同的首页配置项！");
        }
        return homeMapper.update(home) > 0;
    }

    @Override
    public Home selectById(Integer id) {
        return homeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<HomeGoodsVO> selectGoodsVOList(HashMap map) {
        List<HomeGoodsVO> homeGoodsVOList = new ArrayList<>();
        List<HashMap<String, Object>> mapList = homeMapper.selectGoodsVOList(map);

        //Map数组转Bean数组
        for (HashMap<String, Object> one : mapList) {
            HomeGoodsVO vo = BeanUtils.mapToBean(one, HomeGoodsVO.class);
            if (ObjectUtil.isNotNull(vo)) {
                homeGoodsVOList.add(vo);
            }
        }
        return homeGoodsVOList;
    }

    @Override
    public List<HomeCarouselVO> selectCarouselVOList(HashMap map) {
        List<HomeCarouselVO> carouselVoList = new ArrayList<>();
        List<HashMap<String, Object>> mapList = homeMapper.selectCarouselVOList(map);

        //Map数组转Bean数组
        for (HashMap<String, Object> one : mapList) {
            HomeCarouselVO vo = BeanUtils.mapToBean(one, HomeCarouselVO.class);
            if (ObjectUtil.isNotNull(vo)) {
                carouselVoList.add(vo);
            }

        }
        return carouselVoList;
    }

    private Home getByTypeIdAndGoodsId(Integer typeId, Integer goodsId) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("typeId", typeId);
        map.put("goodsId", goodsId);
        return homeMapper.selectByMap(map);
    }

}
